import { http } from '@/utils/request'
import type { AllowQueryParams, DataSopeParams, DeptTree, Role, RoleParams, RoleQueryParams } from './type'
import type { User } from '../user/type'

const prefix = import.meta.env.VITE_API_SYS

// 查询角色列表
export function listRole(params: RoleQueryParams & Page) {
	return http.get<ResPage<Role[]>>(`${prefix}/role/list`, params)
}

// 查询角色详细
export function getRole(roleId: number) {
	return http.get<Role>(`${prefix}/role/${roleId}`)
}

// 新增角色
export function addRole(data: RoleParams) {
	return http.post(`${prefix}/role`, data)
}

// 修改角色
export function updateRole(data: RoleParams) {
	return http.put(`${prefix}/role`, data)
}

// 角色数据权限
export function dataScope(data: DataSopeParams) {
	return http.put(`${prefix}/role/dataScope`, data)
}

// 角色状态修改
export function changeRoleStatus(roleId: number, status: number) {
	return http.put(`${prefix}/role/changeStatus`, {
		id: roleId,
		status
	})
}

// 删除角色
export function delRole(roleId: number) {
	return http.delete(`${prefix}/role/${roleId}`)
}

// 查询角色已授权用户列表
export function allocatedUserList(query: AllowQueryParams & Page) {
	return http.get<ResPage<User[]>>(`${prefix}/role/authUser/allocatedList`, query)
}

// 查询角色未授权用户列表
export function unallocatedUserList(query: { userName: string; phoneNumber: string } & Page) {
	return http.get<ResPage<User[]>>(`${prefix}/role/authUser/unallocatedList`, query)
}

// 取消用户授权角色
export function authUserCancel(data: any) {
	return http.put(`${prefix}/role/authUser/cancel`, data)
}

// 批量取消用户授权角色
export function authUserCancelAll(data: { roleId: string; userIds: string }) {
	return http.put(`${prefix}/role/authUser/cancelAll`, undefined, {
		params: data
	})
}

// 授权用户选择
export function authUserSelectAll(data: { roleId: string; userIds: string }) {
	return http.put(`${prefix}/role/authUser/selectAll`, undefined, {
		params: data
	})
}

// 根据角色ID查询部门树结构
export function deptTreeSelect(roleId: number) {
	return http.get<DeptTree>(`${prefix}/role/deptTree/${roleId}`)
}

// 导出
export function roleExport(data: RoleQueryParams & Page) {
	return http.postFile<ExportResponse>(`${prefix}/role/export`, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		responseType: 'blob'
	})
}
